package com.pyk.controller;

import com.pyk.dto.CreateHQDto;
import com.pyk.entity.Homework;
import com.pyk.entity.Question;
import com.pyk.entity.Result;
import com.pyk.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;

    // 查询作业
    @GetMapping
    public Result list(){
        log.info("查询所有作业信息");
        List<Homework> homeworkList = homeworkService.list();
        return Result.success();
    }

    // 新增作业
    @PostMapping("/insert")
    public Result add(@RequestBody Homework homework){
        log.info("新增作业：{}",homework);
        homeworkService.add(homework);
        return Result.success();
    }

    // 删除作业,并将关联表删除
    @DeleteMapping("/{homeworkId}")
    public Result deleteHomeworkById(@PathVariable Integer homeworkId){
        log.info("根据id删除作业：{}",homeworkId);
        homeworkService.deleteHomeworkById(homeworkId);
        return Result.success();
    }

    // 修改作业
    @GetMapping("{homeworkId}")
    public Result getById(@PathVariable Integer homeworkId){
        log.info("查询作业id：{}",homeworkId);
        Homework homework = homeworkService.getById(homeworkId);
        return Result.success(homeworkId);
    }

    @PutMapping
    public Result update(@RequestBody Homework homework){
        log.info("修改作业：{}",homework);
        homeworkService.update(homework);
        return Result.success();
    }

    // 修改作业，将作业中题目改变（可以修改数据库中的题目信息）
    @DeleteMapping("/{homeworkId}/{questionId}")
    public Result deleteHwQt(@PathVariable Integer homeworkId,@PathVariable Integer questionId){
        log.info("删除作业中的题目");
        homeworkService.deleteHwQt(homeworkId,questionId);
        return Result.success();
    }

    @PutMapping("/{homework}/{questionId}")
    public Result updateHomeworkQ(@PathVariable Integer homeworkId,@PathVariable Long questionId,@RequestBody Question updatedQuestion){
        updatedQuestion.setQuestionId(questionId);
        homeworkService.updateHomeworkQ(homeworkId,questionId,updatedQuestion);
        return Result.success();
    }

    // 作业加入题目
    @PostMapping("/add")
    public Result addHomeworkWithQuestion(@RequestBody CreateHQDto createHQDto){
        log.info("将题目加入作业当中");
        Homework homework = createHQDto.getHomework();
        List<Question> questions = createHQDto.getQuestions();
        int totalScorePerQuestion = createHQDto.getTotalScorePerQuestion();
        homeworkService.createHomeworkWithQuestions(homework, questions,totalScorePerQuestion);
        return Result.success();
    }


}
